<?php

class SerialController extends AdminController {

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionView()
    {
        $session=Yii::app()->session;
        $model_member=new sys_member;
        $model_serial=new sys_serial;
        $model_network=new sys_network;
        $model_serial_products=new sys_serial_products;
        
        $model_serial_buyer=new sys_serial_buyer('search_serial_report');
        $model_serial_buyer->unsetAttributes();
        if(isset($_GET['sys_serial_buyer']))
        {
            $model_serial_buyer->attributes=$_GET['sys_serial_buyer'];
        }
        
        //SET TYPE DOWNLOAD FILE 
       if(isset($_POST['button']))
       {
           
            $dataProvider=$session['data_export']->data;
           $button=$_POST['button'];
           foreach($button AS $key=>$val)
           {
               $type=$key;
           }
        
           //$dataProvider=$model_serial_buyer->search_serial_report();
           if($dataProvider)
           {
               foreach($dataProvider AS $val)
               {
                   $filter_data[]=array(
                       'Tanggal'=>$val->serial_buyer_datetime,
                       'Member Id'=>$val->rel_buyer_to_network->network_mid,
                       'Nama Member'=>$val->rel_buyer_to_network->rel_network_to_member->member_name,
                       'Pasif'=>$val->count_pasive,
                       'Aktif'=>$val->count_active,
                       'Total'=>($val->count_pasive+$val->count_active),
                   );
               }
           }
                if(isset($filter_data) AND is_array($filter_data))
                {
                   switch ($type)
                   {
                        case 'xls':
                            ExcelExporterCustom::sendAsXLS($filename='Data Penjualan Serial', $filter_data, 'Data Penjualan Serial', $header = true);
                        break;

                        default:
                            die('Please make sure your choice. ');
                        break;
                   }
                }
                
           
           
          
       }
//        if(isset($_GET['sys_member']))
//        {
//            $model_member->attributes=$_GET['sys_member'];
//        }
//        
//        if(isset($_GET['sys_network']))
//        {
//            $model_network->attributes=$_GET['sys_network'];
//        }
        
        
        

        $this->render('view', array(
            'model_member'=>$model_member,
            'model_serial'=>$model_serial,
            'model_serial_products'=>$model_serial_products,
            'model_network'=>$model_network,
            'model_serial_buyer'=>$model_serial_buyer,
        ));
  

  }
    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'site-support-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
